// pages/manage/manage.js
const db = wx.cloud.database()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    user_id : "",
    dname: "",
    updName: "",
    updPhone: "",
    udpTpye: "",
    modalHidden: true,
    casArray: ["红卡","黑卡","金卡"],
    casIndex: 0,
  },
  //删除会员弹窗
  showModal(){
    this.setData({
      modalHidden: false
    })
  },
  modalCandel(){
    this.setData({
      modalHidden: true
    })
  },
  //修改会员获取原来的数据
  get_db(user_id){
    var that = this
    db.collection("User").where({
      openid: user_id
    }
  ).get({
    success: function(res){
      that.setData({
        dname : res.data[0].name,
        updName : res.data[0].name,
        updPhone : res.data[0].phoneNumber,
        udpTpye : res.data[0].phoneNumber
      })
    }
  })
  db.collection("VIPSales").where({
    _openid: user_id
  }
  ).get({
    success(re){
      var a = 0
      switch(re.data[0].VIPtype){
        case "红卡":
          a=0;
          break;
        case "黑卡":
          a=1;
          break;
        case "金卡":
          a=2;
          break;
        default:
          a=0;
      }
      that.setData({
        casIndex: a
      })
    }
  })
  },
  
   //删除数据
   delVIP: function(){
    db.collection('VIPSales').where({
      openid: this.data.user_id
    }).remove({
      success: function (res) {
        console.log("删除成功",res)
        wx.showToast({
         title: '删除成功',
         icon: 'success',
         duration: 1000
       });
       },
       fail: function(res){
         console.log("删除失败",res)
         wx.showToast({
          title: '删除失败，请检查输入',
          icon: 'none',
          duration: 1000
        });
       }
    })
    this.setData({
      modalHidden: true
    })
   },
  
  //获取修改时输入的数据
   updName(e) {
    console.log(e.detail.value)
    if(e.detail.value!=''){
      this.setData({
        updName: e.detail.value
      })
    }
   },
   updPhone(e) {
    console.log(e.detail.value)
    if(e.detail.value!=''){
      this.setData({
        updPhone: e.detail.value
      })
    }
   },
   bindCasPickerChange(e){
    this.setData({
      casIndex: e.detail.value,
    })
    var a = ""
    console.log(this.data.casIndex)
    switch (this.data.casIndex) {
      case "0":
        a = "红卡"
        break;
      case "1":
        a = "黑卡"
        break;
      case "2":
        a = "金卡"
        break;
      default:
        break;
    }
    console.log(a)
    this.setData({
      updType: a
    })
   },
   //修改数据
   updVIP:function(){
    //  if(updcheck()){
      var that = this
      db.collection('User').where({
        openid: that.data.user_id
      }).update({
      data:{
        name: that.data.updName,
        phoneNumber: that.data.updPhone,
      },
      success: function (res) {
       console.log("修改成功",res)
       db.collection('VIPSales').where({
        _openid: that.data.user_id
      }).update({
        data:{
          VIPtype: that.data.updType
        },
        success: function (res) {
          console.log("修改成功",res)
          wx.showToast({
           title: '修改成功',
           icon: 'success',
           duration: 1000
         });
         },
         fail: function(res){
           console.log("修改失败",res)
         }
      })
      },
      fail: function(res){
        console.log("修改失败",res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      user_id : options.user_id
    })
    this.get_db(this.data.user_id)
    console.log(this.data.user_id,"12")
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})